import cv2
import numpy as np

if __name__ == "__main__":
    path = 'image.jpg'
    image_np = cv2.imread(path)
    # print(image_np)
    image_np_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
    image_np_f = np.zeros((255, 1), dtype=np.uint16)
    image_np_N0 = np.zeros((255, 1), dtype=np.uint16)
    # print(image_np_gray)
    # print(image_np_f)
    for i in range(0, 255):
        n = 0
        for j in range(image_np_gray.shape[0]):
            for k in range(image_np_gray.shape[1]):
                if image_np_gray[j, k] == i:
                    n += 1
        image_np_f[i] = n
    print(image_np_f)
    for l in range(0, 256):
        N0 = np.sum(image_np_f[0:l + 1])
        # image_np_N0[l] = np.sum(image_np_f[0:l + 1])
        w0 = N0 / np.sum(image_np_f)
        w1 = 1 - w0
        u0 = 0
        for m in range(0, l + 1):                # 便利前l项求前l项平均像素值
            u0 += m * image_np_f[m]
            



    # for i in range(image_np_gray.shape[0]):
    #     for j in range(image_np_gray.shape[1]):
